<?php
declare (strict_types=1);

namespace app\admin\controller\system;

use app\admin\controller\CommonController;
use ffhome\frame\service\SystemConfigService;
use think\facade\Cache;
use think\facade\Db;

class Config extends CommonController
{
    protected $modelName = SystemConfigService::NAME;

    public function index()
    {
        return $this->fetch();
    }

    public function save()
    {
        $data = $this->request->param();
        Db::transaction(function () use ($data) {
            foreach ($data as $key => $val) {
                Db::name($this->modelName)->where('name', $key)->update(['value' => $val,]);
            }
        });
        $this->clearCache();
        $this->success(lang('common.save_success'));
    }

    protected function clearCache()
    {
        parent::clearCache();
        Cache::tag('authority')->clear();
    }
}